<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.io.*" %>
<%@ page import="jsp.util.ConnectionContext" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="java.io.File" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Main</title>
 <link rel=stylesheet href="form.css" type="text/css">
</head>

<%
String savePath = application.getRealPath("/");
String saveFolder="C:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\hello_kitty2-\\WebContent\\filestorage";
String realFolder="";

ServletContext context = getServletContext();
realFolder = context.getRealPath(saveFolder);

int sizeLimit=5*1024*1024;
DefaultFileRenamePolicy policy = new DefaultFileRenamePolicy();

MultipartRequest multi= new MultipartRequest(request, saveFolder, sizeLimit,"utf-8", policy);
File fileName = multi.getFile("photo");

String realfileName = multi.getFilesystemName("photo");
realfileName=saveFolder+'\\'+realfileName;
String originFileName = multi.getOriginalFileName("photo");



//String url = "jdbc:mysql://localhost/dbmember";

//Class.forName("com.mysql.jdbc.Driver");
//long fileSize=file.length();

%>


<%
	String Prof_number=multi.getParameter("hiddenvalue");
	String writer=multi.getParameter("student_name");
	String writer_number=multi.getParameter("student_number");
	String title=multi.getParameter("title");
	String content=multi.getParameter("contents");
	String numbering=multi.getParameter("hiddennumbering");
	int count=0;
	
	try{
		Connection conn = ConnectionContext.getConnection();
		PreparedStatement pstmt = conn.prepareStatement("INSERT INTO studentQ(studName,studNum,profNum,title,registerDate,count,content,numbering,filepath)"+ " values (?,?,?,?,now(),?,?,?,?)");				
		pstmt.setString(1, writer);
		pstmt.setString(2, writer_number);
		pstmt.setString(3, Prof_number);
		pstmt.setString(4, title);
		pstmt.setInt(5, 0);
		pstmt.setString(6, content);
		pstmt.setString(7, numbering);
		pstmt.setString(8, realfileName);
		pstmt.executeUpdate();
	}catch(SQLException ex){
%>
	에러: <%=ex %>
<%

	}
	Connection conn = ConnectionContext.getConnection();
	PreparedStatement pstmt = conn.prepareStatement("select * from studentQ where numbering=?");
	pstmt.setString(1, numbering);
	ResultSet rs = pstmt.executeQuery(); 
	
	while(rs.next()){

%>

<body>

<div id="wrapper" style="border:1px solid #696">
	<jsp:include page="Menubar.jsp" flush="false"/>	

<center>
<p></p> 
<font size='3'><b> Q & A </b></font>
<p></p>             
</center>

<TABLE border="0" width="600" cellpadding="2" cellspacing="2" align="center">
	<TR>
      		<TD>
         		<hr size='1' noshade>
      		</TD>
	</TR>
</TABLE>


<TABLE border="0" width="600" cellpadding="2" cellspacing="2" align="center">
<tr>
    <TD width="100" bgcolor="cccccc" align=center><font size="2">Title</font></td>
    <td colspan="3"><%=rs.getString("title")%></td>
</tr>

<tr>
    <TD width="100" bgcolor="cccccc" align=center><font size="2">Writer</font></td>
    <td colspan="3"><%=rs.getString("studName") %></td>
</tr>
<tr>
	<TD width="100" bgcolor="cccccc" align=center><font size="2">학번</font></td>
	<td><%=rs.getString("studNum")%></td>
	<TD width="100" bgcolor="cccccc" align=center><font size="2">교수님번호</font></td>
	<td><%=Prof_number%></td>
</tr>

<tr>
    <TD width="100" bgcolor="cccccc" align=center><font size="2">Date</font></td>
    <td><%=rs.getDate("registerDate")%></td>
    <TD width="100" bgcolor="cccccc" align=center><font size="2">Count</font></td>
    <td><%=count%></td>
</tr>

<% 
	String filepath=rs.getString("filepath");
	
	if(filepath != null)
	{
%>
<tr>
	<TD width="100" bgcolor="cccccc" align=center><font size="2">photo</font></td>
	<td colspan="3"><img src="<%=filepath%>" border="0" width='600' height='300'></td>
</tr>
<tr>
	<td><%=rs.getString("filepath")%>
	</td>
</tr>
<% 
} 
%>
	<tr>    	
    <TD width="100" bgcolor="cccccc" align=center><font size="2">Content</font></td>
    <td colspan="3"><textarea cols='70' rows='15' name='contents' readonly><%=rs.getString("content")%></textarea></td>
</tr>

</table>
<%
}
%>

<script language="javascript">
	function buttonclick(){
		
		history.go(-2);
		location.reload();
		
	}

</script>


<a href="QandA.jsp"><input type="button" value="목록" onclick="buttonclick()"></a>
</div>
</body>
</html>

